package com.bestcem.xm.user.mq.receive;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;

/**
 * 用户模块通用队列监听
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/16 7:01
 */
//@Slf4j
//@Component
@Deprecated
public class UserCommonMqReceive {

    //@Resource
    //private UserMqConsumeService userMqConsumeService;
    //@Resource
    //private OrganizationQuotainfoService organizationQuotainfoService;

    /**
     * 监听 用户模块通用队列
     *
     * @param message
     * @param channel
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/7/17 7:03
     */
    //@RabbitListener(queues = "#{appUserCommonQueue.name}", concurrency = "${app.user.rabbitmq.appUserCommonQueue.concurrency:10}")
    //@RabbitHandler
    public void commonQuenueHandler(Message message, Channel channel) {
        //try {
        //    if (log.isInfoEnabled()) {
        //        log.info("[USER] 用户模块通用队列收到消息:{}", Base64.getEncoder().encodeToString(dto.getBody()));
        //    }
        //    MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(dto.getBody());
        //    if (mqMessage == null) {
        //        log.error("[USER] 用户模块通用队列消息解析失败:" + Base64.getEncoder().encodeToString(dto.getBody()));
        //        channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
        //        return;
        //    }
        //    if (log.isInfoEnabled()) {
        //        log.info("[USER] 用户模块通用队列消息体 {}", JSON.toJSONString(mqMessage));
        //    }
        //
        //    if (OrganizationTopics.ORG_CREATED.equals(mqMessage.getTopic())) {
        //        // 创建公司
        //        userMqConsumeService.orgCreateMessageListener(MsgPackFormatUtil.mapToObj(mqMessage.getData(), OrgCreateMessage.class));
        //    } else if (UserTopics.USER_QUOTA_CONSUMPTION.equals(mqMessage.getTopic())) {
        //        // 用户中心公司限额数增加
        //        QuotaConsumptionMessage quotaConsumptionMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(), QuotaConsumptionMessage.class);
        //        QuotaConsumptionDTO quotaConsumptionDTO = new QuotaConsumptionDTO();
        //        quotaConsumptionDTO.setOrgId(quotaConsumptionMessage.getOrgId());
        //        quotaConsumptionDTO.setAmount(quotaConsumptionMessage.getAmount());
        //        quotaConsumptionDTO.setType(quotaConsumptionMessage.getType());
        //        organizationQuotainfoService.updateQuotaConsumption(quotaConsumptionDTO);
        //    } else if (UserTopics.USER_QUOTA_REACHED.equals(mqMessage.getTopic())) {
        //        // 用户中心公司达到限额通知
        //        if (log.isInfoEnabled()) {
        //            log.info("------------MQ:user.quota_reached  用户中心公司达到限额通知----------------");
        //        }
        //        QuotaReachedMessage quotaReachedMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(), QuotaReachedMessage.class);
        //        QuotaReachedDTO quotaReachedDTO = new QuotaReachedDTO();
        //        BeanUtils.copyProperties(quotaReachedMessage, quotaReachedDTO);
        //        organizationQuotainfoService.notifyQuotaReachedToUser(quotaReachedDTO);
        //    }
        //
        //    channel.basicAck(dto.getMessageProperties().getDeliveryTag(), false);
        //} catch (Exception e) {
        //    String errorMsg = Base64.getEncoder().encodeToString(dto.getBody());
        //    log.error("[USER] 用户模块通用队列消息处理异常 {}", errorMsg, e);
        //    try {
        //        channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
        //    } catch (IOException ex) {
        //        log.error("[USER] 用户模块通用队列消息确认失败", ex);
        //    }
        //}
    }

}
